/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.report;

import java.util.HashMap;

/**
 * AbstractExpAttributeHandler only handler the expand attribute of the TimeConsume for AbstractReportBean.
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 */
public abstract class AbstractExpAttributeHandler implements
		IExpAttributeHandler {
	public static String EXPATTR_TIMECONSUME = "TimeConsume";

	protected Object source;

	protected HashMap oldValue = new HashMap();

	protected HashMap expAttributes = new HashMap();

	public HashMap handlerExpAttributes(Object source, Object value) {
		if (HashMap.class.isInstance(value)) {
			HashMap map = (HashMap) value;
			if (map.containsKey(EXPATTR_TIMECONSUME)) {
				String newValue = (String)map.get(EXPATTR_TIMECONSUME);
				int timeConsume = Integer.valueOf(newValue).intValue();
				if(source.equals(this.source)){
					int oldTc = Integer.valueOf((String)oldValue.get(EXPATTR_TIMECONSUME)).intValue();
					timeConsume = timeConsume-oldTc;
				}
				this.source = source;
				this.oldValue.put(EXPATTR_TIMECONSUME, newValue);
				doHandlerTimeConsume(timeConsume, EXPATTR_TIMECONSUME);
			}
		}
		return this.expAttributes;
	}

	private void doHandlerTimeConsume(int timeConsume, String key) {
		Object oldValue = this.expAttributes.get(key);
		int oldTimeConsume = 0;
		if (oldValue == null) {
			oldTimeConsume = timeConsume;
		} else {
			oldTimeConsume = Integer.valueOf(String.valueOf(oldValue))
					.intValue()
					+ timeConsume;
		}
		this.expAttributes.put(EXPATTR_TIMECONSUME, String
				.valueOf(oldTimeConsume));
	}

}
